package cn.com.dashihui.web.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.ExcelExportUtil;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.dao.AdSeller;
import cn.com.dashihui.web.dao.GSR;
import cn.com.dashihui.web.dao.Seller;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.dao.User;

public class SellerService {

	/**
	 * 分页查询公司
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public Page<Record> findByPage(int pageNum, int pageSize,String sellerId,String beginDate,String endDate,int state,String tel) {
		StringBuffer sBuffer = new StringBuffer(" FROM t_bus_seller T inner join  t_auth_admin_seller A on T.userid=A.id WHERE 1=1 ");
		
		List<Object> params = new ArrayList<Object>();
		if(!StrKit.isBlank(sellerId)){
			sBuffer.append(" AND T.sellerId=?");
			params.add(sellerId);
		}
		if(!StrKit.isBlank(beginDate)&&!StrKit.isBlank(endDate)){
			sBuffer.append(" AND T.createtime BETWEEN ? AND ?");
			params.add(beginDate);
			params.add(endDate);
		}else if(!StrKit.isBlank(beginDate)&&StrKit.isBlank(endDate)){
			sBuffer.append(" AND DATE_FORMAT(T.createtime,'%Y-%m-%d')>=?");
			params.add(beginDate);
		}else if(StrKit.isBlank(beginDate)&&!StrKit.isBlank(endDate)){
			sBuffer.append(" AND DATE_FORMAT(T.createtime,'%Y-%m-%d')<=?");
			params.add(endDate);
		}
		if(StrKit.notNull(state)&&state!=0){
			sBuffer.append(" AND T.state=?");
			params.add(state);
		}
		if(!StrKit.isBlank(tel)){
			sBuffer.append(" AND T.contact=?");
			params.add(tel);
		}
		sBuffer.append(" ORDER BY T.createtime DESC,T.id desc");
		return  Db.paginate(pageNum, pageSize, "SELECT T.id,T.sellerId,A.trueName,T.contact,T.sellerName,T.createtime,T.checktime,T.state", sBuffer.toString(),params.toArray());
	}
	public List<Store> findStoreBySeller(String sellerId) {
		return Store.me().find("select * from t_dict_store where sellerId=?",sellerId);
	}
	
	/**
	 * 查询公司详情
	 * @param id
	 * @return
	 */
	public Record detail(String sellerId) {
		return Db.findFirst("select * from t_bus_seller where sellerId=?",sellerId);
	}
	public int doCancel(String reason,String sellerId,String checktime) {
		return Db.update("update t_bus_seller set state=3 ,checktime=?,reason=? where sellerId=?",checktime,reason,sellerId);
	}
	/**
	 * 公司降权
	  *<p>Title: doDrop</p> 
	  *<p>Description: </p> 
	  * @param reason
	  * @param sellerId
	  * @param checktime
	  * @return 
	 *
	 */
	public int doDrop(String reason,String sellerId,String checktime) {
		return Db.update("update t_bus_seller set state=5 ,checktime=?,reason=? where sellerId=?",checktime,reason,sellerId);
	}
	/*public int doAccept(String sellerId,String checktime) {
		return Db.update("update t_bus_seller set state=2 ,checktime=? where sellerId=?",checktime,sellerId);
	}*/
	public Seller findBySellerId(String sellerId) {
		return Seller.me().findFirst("select * from t_bus_seller where sellerId=?",sellerId);
	}
	public boolean doDelete(Seller seller) {
		return seller.delete();
	}
	public Seller findPageBySellerId(String sellerId) {
		return Seller.me().findFirst("SELECT T.id,T.sellerId,A.trueName,T.contact,T.sellerName,T.createtime,T.checktime,T.state "
				+ " FROM t_bus_seller T inner join  t_auth_admin_seller A on T.userid=A.id WHERE T.sellerId=?",sellerId);
	}
	public List<GSR> finds(String storeid) {
		return GSR.me().find("select * from t_bus_goods_store_rel where storeid=?",storeid);
	}
	/**
	 * 查找公司
	 * @return
	 */
	public List<Seller> findSellerList() {
		return Seller.me().find("select sellerId,sellerName from t_bus_seller");
	}
	/**
	 * 导出数据
	 * @param response
	 * @param request
	 * @param listz 
	 * @param titles2 
	 * @param member
	 */
	public void export(HttpServletResponse response, HttpServletRequest request, List<Pair> titles, List<Record> listz) {
		ExcelExportUtil.exportByRecord(response, request, "公司入驻列表", titles, listz);
	}
	public List<Record> findExcelByPage(String sellerId, String beginDate, String endDate, int state, String tel) {
		StringBuffer sBuffer = new StringBuffer("SELECT T.id,T.sellerId,A.trueName,T.contact,T.sellerName,T.createtime,T.checktime,T.state FROM t_bus_seller T inner join  t_auth_admin_seller A on T.userid=A.id WHERE 1=1");
		sBuffer.append(" ORDER BY T.createtime DESC,T.id desc");
		List<Object> params = new ArrayList<Object>();
		if(!StrKit.isBlank(sellerId)){
			sBuffer.append(" AND T.sellerId=?");
			params.add(sellerId);
		}
		if(!StrKit.isBlank(beginDate)&&!StrKit.isBlank(endDate)){
			sBuffer.append(" AND T.createtime BETWEEN ? AND ?");
			params.add(beginDate);
			params.add(endDate);
		}else if(!StrKit.isBlank(beginDate)&&StrKit.isBlank(endDate)){
			sBuffer.append(" AND DATE_FORMAT(T.createtime,'%Y-%m-%d')>=?");
			params.add(beginDate);
		}else if(StrKit.isBlank(beginDate)&&!StrKit.isBlank(endDate)){
			sBuffer.append(" AND DATE_FORMAT(T.createtime,'%Y-%m-%d')<=?");
			params.add(endDate);
		}
		if(StrKit.notNull(state)&&state!=0){
			sBuffer.append(" AND T.state=?");
			params.add(state);
		}
		if(!StrKit.isBlank(tel)){
			sBuffer.append(" AND A.phonenumber=?");
			params.add(tel);
		}
		return  Db.find( sBuffer.toString(),params.toArray());
	}
	public AdSeller findSeller(int userid) {
		return AdSeller.me().findFirst("select * from t_auth_admin_seller where id=?",userid);
	}
	/**
	 * 根据电话号码获取用户对象
	 * @param phone
	 * @return
	 */
	public User findUserByPhone(String phone){
		return User.me().findFirst("SELECT * from t_bus_user where phone=?", phone);
	}
	/**
	 * 商家，以及分公司强制下线
	 */
	public int doDown(String downtime,String sellerId){
		return Db.update("update t_bus_seller set state=4,downtime=? where sellerId=?",downtime,sellerId);
	}
	/**
	 * 下架所有商品
	 */
	public int downGoods(String goodstore){
		return Db.update("update t_bus_goods set state=3 where good_store=?", goodstore);
	}
	/**
	 * 下线所有分公司
	 */
	public int downStore(String storeid){
		return Db.update("update t_dict_store set state=3 where storeid=?", storeid);
	}
}
